{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先 import 必要的模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import lightgbm as lgbm\n",
    "#from lgbm.sklearn import LGBMClassifier\n",
    "import xlearn as xl\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "import time\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "from sklearn.metrics import log_loss     #采用logloss作为评价指标\n",
    "from pandas import DataFrame\n",
    "\n",
    "from sklearn.metrics import log_loss\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from matplotlib import pyplot\n",
    "import seaborn as sns\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_name_1='400_0'\n",
    "train_name_2='400_1'\n",
    "valid_name_1='400_2'\n",
    "valid_name_2='400_3'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3996246, 23)\n",
      "(3886316, 23)\n"
     ]
    }
   ],
   "source": [
    "#train_data\n",
    "train_1 = pd.read_csv(\"/home/jupyter/训练数据组/400w数据组/\"+train_name_1,index_col=0).reset_index(drop=True)   #拿200万条数据训练\n",
    "train_1.drop(['id','hour','user_id','Unnamed: 0'],axis=1,inplace=True)\n",
    "print(train_1.shape)\n",
    "#validation_data\n",
    "valid_1 = pd.read_csv(\"/home/jupyter/训练数据组/400w数据组/\"+valid_name_1,index_col=0).reset_index(drop=True)   #拿200万条数据训练\n",
    "valid_1.drop(['id','hour','user_id','Unnamed: 0'],axis=1,inplace=True)\n",
    "print(valid_1.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(999864, 23)\n",
      "(996224, 23)\n"
     ]
    }
   ],
   "source": [
    "#train_data\n",
    "train_2 = pd.read_csv(\"/home/jupyter/训练数据组/100w数据组/\"+train_name_2,index_col=0).reset_index(drop=True)   #拿200万条数据训练\n",
    "train_2.drop(['id','hour','user_id','Unnamed: 0'],axis=1,inplace=True)\n",
    "print(train_2.shape)\n",
    "valid_2 = pd.read_csv(\"/home/jupyter/训练数据组/100w数据组/\"+valid_name_2,index_col=0).reset_index(drop=True)   #拿200万条数据训练\n",
    "valid_2.drop(['id','hour','user_id','Unnamed: 0'],axis=1,inplace=True)\n",
    "print(valid_2.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/qiaoliang/anaconda3/lib/python3.6/site-packages/numpy/lib/arraysetops.py:472: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n",
      "  mask |= (ar1 == a)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(4218938, 23)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#test_data\n",
    "test = pd.read_csv(\"/home/jupyter/训练数据组/FE_test_26_final.csv\",index_col=0).reset_index(drop=True)   #拿200万条数据训练\n",
    "test.drop(['id','hour','user_id'],axis=1,inplace=True)\n",
    "test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "#first\n",
    "y_train_1 = train_1['click']\n",
    "x_train_1 = train_1.drop([\"click\"], axis=1)\n",
    "\n",
    "y_valid_1 = valid_1['click']\n",
    "x_valid_1 = valid_1.drop([\"click\"], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#second\n",
    "y_train_2 = train_2['click']\n",
    "x_train_2 = train_2.drop([\"click\"], axis=1)\n",
    "\n",
    "y_valid_2 = valid_2['click']\n",
    "x_valid_2 = valid_2.drop([\"click\"], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_test = test['click']\n",
    "x_test = test.drop([\"click\"], axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 original_data generate libffm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "#fm_data\n",
    "x_train_fm_1=np.array(x_train_1)\n",
    "x_valid_fm_1=np.array(x_valid_1)\n",
    "x_test_fm_1=np.array(x_test)\n",
    "xl.write_data_to_xlearn_format(X=x_train_fm_1,y=y_train_1,filepath=r'/home/jupyter/xlearn/fm/fm_train.txt')\n",
    "xl.write_data_to_xlearn_format(X=x_valid_fm_1,y=y_valid_1,filepath=r'/home/jupyter/xlearn/fm/fm_valid.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xl.write_data_to_xlearn_format(X=x_test_fm_1,y=y_test,filepath=r'/home/jupyter/xlearn/fm/fm_test.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "del train_1\n",
    "del train_2\n",
    "del valid_1\n",
    "del valid_2\n",
    "del test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "默认参数，此时学习率为0.1，比较大，观察弱分类数目的大致范围\n",
    "（采用默认参数配置，看看模型是过拟合还是欠拟合）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "scrolled": true
   },
   "source": [
    "## x-learn train"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.fm模型训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# original Training task\n",
    "fm_model = xl.create_fm()\n",
    "fm_model.setSign()  # Convert output to 0-1  scope = xl.create_fm() # Use field-aware factorization machine\n",
    "fm_model.setTrain(r'/home/jupyter/xlearn/fm/fm_train.txt')  # Training data\n",
    "fm_model.setValidate(r'/home/jupyter/xlearn/fm/fm_valid.txt')  # Validation data\n",
    "fm_model.setTest(r'/home/jupyter/xlearn/fm/fm_test.txt')  # test data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "78.06491708755493\n"
     ]
    }
   ],
   "source": [
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracy\n",
    "start=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.00001, \n",
    "         'metric':'acc','fold':2,'epoch':25,'stop_window':3}\n",
    "\n",
    "fm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=fm_model.fit(param, r'/home/jupyter/xlearn/fm/model.out')\n",
    "result_cv=fm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "time.sleep(5)\n",
    "fm_model.predict(r'/home/jupyter/xlearn/fm/model.out', r'/home/jupyter/xlearn/fm/output.txt')\n",
    "#0.4443,0.4456,0.4422"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "#lambda调整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "94.65050840377808\n"
     ]
    }
   ],
   "source": [
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracy\n",
    "start=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.0001, \n",
    "         'metric':'acc','fold':2,'epoch':25,'stop_window':3}\n",
    "\n",
    "fm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=fm_model.fit(param, r'/home/jupyter/xlearn/fm/model.out')\n",
    "result_cv=fm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)\n",
    "time.sleep(5)\n",
    "fm_model.predict(r'/home/jupyter/xlearn/fm/model.out', r'/home/jupyter/xlearn/fm/output.txt')\n",
    "#0.4468,0.448,0.4457"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "68.1694347858429\n"
     ]
    }
   ],
   "source": [
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracy\n",
    "start=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.000001, \n",
    "         'metric':'acc','fold':2,'epoch':25,'stop_window':3}\n",
    "\n",
    "fm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=fm_model.fit(param, r'/home/jupyter/xlearn/fm/model.out')\n",
    "result_cv=fm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)\n",
    "time.sleep(5)\n",
    "fm_model.predict(r'/home/jupyter/xlearn/fm/model.out', r'/home/jupyter/xlearn/fm/output.txt')\n",
    "#0.4468,0.448,0.4416"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "66.54297685623169\n"
     ]
    }
   ],
   "source": [
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracy\n",
    "start=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.0000001, \n",
    "         'metric':'acc','fold':2,'epoch':25,'stop_window':3}\n",
    "\n",
    "fm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=fm_model.fit(param, r'/home/jupyter/xlearn/fm/model.out')\n",
    "result_cv=fm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)\n",
    "time.sleep(5)\n",
    "fm_model.predict(r'/home/jupyter/xlearn/fm/model.out', r'/home/jupyter/xlearn/fm/output.txt')\n",
    "#0.4468,0.448,0.4413"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "71.16335582733154\n"
     ]
    }
   ],
   "source": [
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracy\n",
    "start=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.00000001, \n",
    "         'metric':'acc','fold':2,'epoch':25,'stop_window':3}\n",
    "\n",
    "fm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=fm_model.fit(param, r'/home/jupyter/xlearn/fm/model.out')\n",
    "result_cv=fm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)\n",
    "time.sleep(5)\n",
    "fm_model.predict(r'/home/jupyter/xlearn/fm/model.out', r'/home/jupyter/xlearn/fm/output.txt')\n",
    "#0.4468,0.448,0.4413"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "#学习率调整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "81.30421829223633\n"
     ]
    }
   ],
   "source": [
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracy\n",
    "start=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.5,'lambda':0.00000001, \n",
    "         'metric':'acc','fold':2,'epoch':25,'stop_window':3}\n",
    "\n",
    "fm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=fm_model.fit(param, r'/home/jupyter/xlearn/fm/model.out')\n",
    "result_cv=fm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)\n",
    "time.sleep(5)\n",
    "fm_model.predict(r'/home/jupyter/xlearn/fm/model.out', r'/home/jupyter/xlearn/fm/output.txt')\n",
    "#0.4468,0.448,0.4413"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
